/*
 * IMLocalStorageP.nc -- local storage component
 *
 * Copyright (C) Andrey Vihrov <andrey.vihrov@gmail.com>, 2010
 * Copyright (C) Askar Salimbaev <ghostinshell@gmail.com>, 2010
 *
 * This file is part of Intermote.
 *
 * Intermote is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Intermote is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Intermote.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * NOTE: IMLocalStorage can have multiple clients, which have to be isolated,
 *       that's why this component is generic
 */

#include <StorageVolumes.h>

generic configuration IMLocalStorageP() {

	provides {
		interface IMBlockStorage as BlockStorage;
		interface IMBlockProperty as BlockProperty[uint8_t];

		// FIXME Only temporary
		interface SplitControl as LocalControl;
	}
}
implementation {
	enum {
		STORAGE_ID = unique( "IMLocalStorage" ),
	};
#ifndef LOCALSTORAGE_PROPER
	/* This part seems to work OK */

	// NOTE: When DirectStorageC will be available in TOS,
	//      Stm25p-dependant BlockStorageExtC should replaced and removed
	components new IMBlockStorageExtC(VOLUME_IMSTORAGE), IMLocalStorageM, IMReplicationP;
	IMLocalStorageM.BlockRead[ STORAGE_ID ] -> IMBlockStorageExtC.BlockRead;
	IMLocalStorageM.BlockWrite[ STORAGE_ID ] -> IMBlockStorageExtC.IMBlockWriteExt;
	IMLocalStorageM.Replication -> IMReplicationP;
	BlockStorage  = IMLocalStorageM.BlockStorage[ STORAGE_ID ];

	// An ugly hack
	//        components IMLocalBlockPropertyM;
	//	BlockProperty = IMLocalBlockPropertyM.BlockProperty;

	BlockProperty[IM_PROP_NEXT_ID] = IMLocalStorageM.BlockProperty[IM_PROP_NEXT_ID, STORAGE_ID]; // <-- ZOMG

	// Init
	components MainC;
	MainC.SoftwareInit -> IMLocalStorageM.Init[STORAGE_ID];

	components IMLibP;

	IMLocalStorageM.LocalAddress -> IMLibP;
	IMLocalStorageM.BlockId      -> IMLibP;

	LocalControl = IMLocalStorageM.LocalControl[STORAGE_ID];

#else
	/* Happy debugging */

	// NOTE: When DirectStorageC will be available in TOS,
	//      Stm25p-dependant BlockStorageExtC should replaced and removed
	components new IMBlockStorageExtC(VOLUME_IMSTORAGE), IMBlockStorageM, IMReplicationP;
	IMBlockStorageM.BlockRead   -> IMBlockStorageExtC.BlockRead;
	IMBlockStorageM.BlockWrite  -> IMBlockStorageExtC.IMBlockWriteExt;
	IMBlockStorageM.Replication -> IMReplicationP;

	components IMLocalStorageProxyM, new QueueC(storageRequest_t,IM_STORAGE_QUEUE_SIZE);
	IMLocalStorageProxyM.LocalStorage  -> IMBlockStorageM.BlockStorage;
	IMLocalStorageProxyM.LocalProperty -> IMBlockStorageM.BlockProperty;
	IMLocalStorageProxyM.Queue -> QueueC;
	BlockStorage  = IMLocalStorageProxyM.BlockStorage[ STORAGE_ID ];
	BlockProperty[IM_PROP_NEXT_ID] = IMLocalStorageM.BlockProperty[IM_PROP_NEXT_ID, STORAGE_ID]; // <-- ZOMG

	//components IMLocalBlockPropertyM;
	//BlockProperty = IMLocalBlockPropertyM.BlockProperty;

	// Init
	components MainC;
	MainC.SoftwareInit -> IMBlockStorageM.Init;

	components IMLibP;

	IMBlockStorageM.LocalAddress -> IMLibP;
	IMBlockStorageM.BlockId      -> IMLibP;

	LocalControl = IMLocalStorageProxyM.LocalControl[STORAGE_ID];

#endif
}
